package com.example.a10.controller;

import com.example.a10.util.AddressConstants;
import com.example.a10.entity.Document;
import com.example.a10.util.DocumentUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.print.Doc;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Test")
public class TestCintroller {

    @ApiOperation("上传测试文件,并返回所有测试文件的所有名称")
    @PostMapping("/Upload")
    public List<Document> TestUpload(List<MultipartFile> file) throws IOException {
        //判断多个文件上传个数是否为0
        if(file!=null){
            for (MultipartFile multipartFile:file){
                if(!multipartFile.isEmpty()){
                    //获取文件名
                    String name = multipartFile.getOriginalFilename();
                    multipartFile.transferTo(new File(AddressConstants.Test+name));
                }
            }
        }
        return selectTestlist();
    }

    @ApiOperation("返回所有训练文件的所有名称")
    @GetMapping("/Testlist")
    @ResponseBody
    public List<Document> selectTestlist(){
        File file = new File(AddressConstants.Test);
        String[] Trainlist = file.list();
        List<Document> documents = new ArrayList<>();
        for (String train:Trainlist){
            Document document = new Document();
            document.setName(train);
            String filesize = DocumentUtil.getFilesize(AddressConstants.Test + train);
            document.setSize(filesize);
            documents.add(document);
        }
        return documents;
    }

    @ApiOperation("返回测试文件名称，删除模型")
    @DeleteMapping("/delete")
    public String deleteTest(@RequestBody Document document){
        if(document!=null){
            String name = document.getName();
            File file = new File(AddressConstants.Test+name);
            file.delete();
            return "success";
        }else {
            return "failed";
        }

    }


}



